using System;
using System.Collections.Generic;
using System.Text;

namespace SharksHunter.UI.Controls.Abstract
{
    public abstract class ParamProvider:SharksHunter.TransferObjects.Presentation.IParamProvider
    {
        private SharksHunter.TransferObjects.Presentation.HunterDialogResult _dialogResult;

        protected System.Windows.Forms.Form _underlyingForm;
        protected System.Windows.Forms.CommonDialog _underlyingCmnDialog;
        private System.Windows.Forms.ErrorProvider _error;

        private string[] _requestedParams;

        public ParamProvider(string[] reqpars, System.Windows.Forms.Form form)
        {
            _underlyingForm = form;
            _requestedParams = reqpars;
            _error = new System.Windows.Forms.ErrorProvider();
            
            _underlyingForm.FormClosing +=new System.Windows.Forms.FormClosingEventHandler(_underlyingForm_FormClosing);

            if(_underlyingForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _dialogResult = SharksHunter.TransferObjects.Presentation.HunterDialogResult.OK;
            }
            else
            {
                _dialogResult = SharksHunter.TransferObjects.Presentation.HunterDialogResult.Cancel;
            }
        }


        public ParamProvider(string[] reqpars, System.Windows.Forms.CommonDialog form)
        {
            _underlyingCmnDialog = form;
            _requestedParams = reqpars;

            Initialize();

            if (_underlyingCmnDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _dialogResult = SharksHunter.TransferObjects.Presentation.HunterDialogResult.OK;
            }
            else
            {
                _dialogResult = SharksHunter.TransferObjects.Presentation.HunterDialogResult.Cancel;
            }
        }

        void _underlyingForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            _error.Clear();

            if (this._underlyingForm.DialogResult == System.Windows.Forms.DialogResult.OK)
            {

                List<string> l = new List<string>(_requestedParams);
                foreach (System.Windows.Forms.Control c in this._underlyingForm.Controls)
                {
                    if (c.Tag != null && l.Contains(c.Tag.ToString()))
                    {
                        if (c.Text == "")
                        {
                            _error.SetError(c, "This field is required");
                            e.Cancel = true;
                        }
                    }
                }

            }
            
        }


        virtual public string[] QueryParam(string param)
        {
            foreach(System.Windows.Forms.Control c in this._underlyingForm.Controls)
            {
                if(c.Tag != null && c.Tag.ToString() == param)
                {
                    return new string[] { c.Text };
                }
            }
            throw new SharksHunter.TransferObjects.Exceptions.ParamNotProvidedException(param);
        }

        virtual public void Initialize()
        { }

        public SharksHunter.TransferObjects.Presentation.HunterDialogResult DialogResult
        {
            get{
                return _dialogResult;
            }
        }
    }
}
